<html>
<head>
<title>problem</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
</head>
<body>
<div>
I want to replace in
<script type="text/javascript">
document.write("the middle");
</script>
of a node
</div>
<script type="text/javascript">
(function() {
    $('div').find('script').detach();
    $('div').html( function(idx, oldhtml) {
        return oldhtml.replace('replace', '<a href="">replace</a>' );
    });
})();
</script>
</body>
</html>
